package com.lin.controller;


import com.lin.pojo.PageVo;
import com.lin.pojo.SysRoles;
import com.lin.pojo.SysUsers;
import com.lin.service.SysUsersService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.HashMap;
import java.util.Map;

@RestController
public class SysUsersController {
    @Autowired
    SysUsersService sysUsersService;

    @RequestMapping("/login.action")
    public int login(SysUsers users, HttpServletRequest request){
        int i=0;
        SysUsers sysUsers=sysUsersService.login(users);
        //Session存值
        HttpSession session=request.getSession();
        session.setAttribute("sysUsers",sysUsers);
        if (sysUsers!=null){
            i=1;
        }
//        //调用service，获取数据
//        List<SysUsers> list=sysUsersService.login(users);
//        //获取集合的第一个对象中的角色id以及角色名存入对象内
//        SysUsers temp=list.get(0);
//        users.setId(temp.getId());
//
//        SysRoles sysRoles=new SysRoles();
//        sysRoles.setName(temp.getSysRoles().getName());
//        users.setSysRoles(sysRoles);
//        List<SysMenus> menulist=new ArrayList<>();
//        for(SysUsers obj : list)
//        {
//            SysMenus temp_menus=new SysMenus();
//            temp_menus.setId(obj.getSysRoles().getSysMenusId());
//            menulist.add(temp_menus);
//        }
//        users.getSysRoles().setSysMenus(menulist);



        return i;
    }
    @PostMapping("/sysusers.action")
    public Map addsysusers(SysUsers sysUsers){
        boolean ad= sysUsersService.save(sysUsers);
        Map map=new HashMap();
        map.put("msg","添加失败");
        if (ad){
            map.put("msg","添加成功");
            map.put("code","1");
        }
        return map;
    }
    @DeleteMapping("/sysusers.action/{id}")
    public Map delsysusers(@PathVariable int id){
        boolean del= sysUsersService.removeById(id);
        Map map=new HashMap();
        map.put("msg","删除失败");
        if(del){
            map.put("msg","删除成功");
        }
        return map;
    }
    @PutMapping("/sysusers.action")
    public Map updatesysusers(@RequestBody SysUsers sysUsers){
        boolean res=sysUsersService.updateById(sysUsers);
        Map map=new HashMap();
        map.put("msg","修改失败");
        if (res){
            map.put("msg","修改成功");
            map.put("code","1");
        }
        return map;
    }
    @GetMapping("/sysusers.action/{id}")
    public SysUsers querysysusersbyid(@PathVariable int id){
        return sysUsersService.getById(id);
    }
    @GetMapping("/sysusers.action")
    public PageVo<SysUsers> querysysusersbycond(SysUsers sysUsers,
                                              @RequestParam(value = "pageno",defaultValue = "1") Integer pageno,
                                              @RequestParam(value = "pagesize",defaultValue = "5")Integer pagesize){
        return sysUsersService.querySysUsersbycond(sysUsers,pageno,pagesize);
    }
}
